/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CompanyPanel.java
 *
 * Created on 27 Jan 2011, 10:49:13 AM
 */
package za.co.pas.business.view.panel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import za.co.pas.business.controller.CompanyDetailController;
import za.co.pas.business.message.MessageObj;
import za.co.pas.business.data.Address;
import za.co.pas.business.data.Company;
import za.co.pas.business.data.UserLoginDetails;
import za.co.pas.business.view.model.list.UserDetailListModel;
import za.co.pas.business.view.model.list.UserListModel;
import za.co.pas.business.abstracts.AbstractView;
import za.co.pas.business.constant.Constant;
import za.co.pas.business.model.ext.UserDetailModel;
import za.co.pas.business.mvc.BusinessMVC;
import za.co.pas.business.view.dlg.AddressDialog;
import za.co.pas.business.view.dlg.UserDialog;

/**
 *
 * @author alabuschagne
 */
public class CompanyDetailPanel extends JPanel implements AbstractView
{

    private boolean dirty = false;
    private UserListModel userListModel = new UserListModel();
    private UserDetailListModel userDetailListModel = new UserDetailListModel();
    private Company newCompany = null;
    //This will be used later on to save the newComapny (controller -> model)
    private CompanyDetailController companyDetailController;

    private CompanyDetailPanel()
    {
    }

    /** Creates new form CompanyPanel */
    public CompanyDetailPanel(CompanyDetailController companyDetailController)
    {
        this.companyDetailController = companyDetailController;
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        companyScrollPane = new javax.swing.JScrollPane();
        jPanel5 = new javax.swing.JPanel();
        companyNamePanel = new javax.swing.JPanel();
        copmanyNameTextField = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        companyTradingAsTextField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        companyEnabledCheckBox = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        companySubscribeCheckBox = new javax.swing.JCheckBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        companyCodeLabel = new javax.swing.JLabel();
        companyAddressPanel = new javax.swing.JPanel();
        streetAddressLabel = new javax.swing.JLabel();
        postalAddressLabel = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        editCompanyStreetAddressButton = new javax.swing.JButton();
        editCompanyPostalAddressButton = new javax.swing.JButton();
        usersPanel = new javax.swing.JPanel();
        jPanel3 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        userDetailsList = new javax.swing.JList();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        companyUserList = new javax.swing.JList();
        updateButton = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        companyNamePanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Name", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        copmanyNameTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                copmanyNameTextFieldKeyPressed(evt);
            }
        });

        jLabel1.setText("Company registered name:");

        companyTradingAsTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                companyTradingAsTextFieldKeyPressed(evt);
            }
        });

        jLabel2.setText("Trading as name:");

        companyEnabledCheckBox.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                companyEnabledCheckBoxMouseReleased(evt);
            }
        });
        companyEnabledCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                companyEnabledCheckBoxStateChanged(evt);
            }
        });

        jLabel3.setText("Enabled:");

        companySubscribeCheckBox.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                companySubscribeCheckBoxMouseReleased(evt);
            }
        });
        companySubscribeCheckBox.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                companySubscribeCheckBoxStateChanged(evt);
            }
        });

        jLabel4.setText("Subscriber:");

        jLabel5.setText("Code:");

        companyCodeLabel.setText("jLabel6");

        javax.swing.GroupLayout companyNamePanelLayout = new javax.swing.GroupLayout(companyNamePanel);
        companyNamePanel.setLayout(companyNamePanelLayout);
        companyNamePanelLayout.setHorizontalGroup(
            companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(companyNamePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(4, 4, 4)
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, companyNamePanelLayout.createSequentialGroup()
                        .addComponent(companyTradingAsTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(companySubscribeCheckBox)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel3))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, companyNamePanelLayout.createSequentialGroup()
                        .addComponent(copmanyNameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 399, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(companyCodeLabel)
                    .addComponent(companyEnabledCheckBox))
                .addContainerGap())
        );
        companyNamePanelLayout.setVerticalGroup(
            companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(companyNamePanelLayout.createSequentialGroup()
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(copmanyNameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(companyCodeLabel)
                    .addComponent(jLabel5))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(companyEnabledCheckBox)
                    .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(companySubscribeCheckBox)
                        .addGroup(companyNamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(companyTradingAsTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))))
                .addContainerGap())
        );

        companyAddressPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Address", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N

        streetAddressLabel.setToolTipText("Right click for menu");
        streetAddressLabel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        streetAddressLabel.setOpaque(true);
        streetAddressLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                streetAddressLabelMouseReleased(evt);
            }
        });

        postalAddressLabel.setToolTipText("Right click for menu");
        postalAddressLabel.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));
        postalAddressLabel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                postalAddressLabelMouseReleased(evt);
            }
        });

        jLabel13.setText("Street address:");

        jLabel14.setText("Postal address:");

        editCompanyStreetAddressButton.setText("...");
        editCompanyStreetAddressButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editCompanyStreetAddressButtonActionPerformed(evt);
            }
        });

        editCompanyPostalAddressButton.setText("...");
        editCompanyPostalAddressButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editCompanyPostalAddressButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout companyAddressPanelLayout = new javax.swing.GroupLayout(companyAddressPanel);
        companyAddressPanel.setLayout(companyAddressPanelLayout);
        companyAddressPanelLayout.setHorizontalGroup(
            companyAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, companyAddressPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(companyAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel14)
                    .addComponent(jLabel13))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(companyAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(postalAddressLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 494, Short.MAX_VALUE)
                    .addComponent(streetAddressLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 494, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(companyAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(editCompanyPostalAddressButton)
                    .addComponent(editCompanyStreetAddressButton))
                .addContainerGap())
        );
        companyAddressPanelLayout.setVerticalGroup(
            companyAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(companyAddressPanelLayout.createSequentialGroup()
                .addGroup(companyAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel13)
                    .addComponent(streetAddressLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(editCompanyStreetAddressButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(companyAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(editCompanyPostalAddressButton)
                    .addGroup(companyAddressPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(postalAddressLabel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel14, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        companyAddressPanelLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {postalAddressLabel, streetAddressLabel});

        usersPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "Users", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N
        usersPanel.setLayout(new java.awt.BorderLayout(10, 0));

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "User details", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N
        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel2.setLayout(new java.awt.BorderLayout());

        userDetailsList.setModel(userDetailListModel);
        userDetailsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        userDetailsList.setToolTipText("Right click for menu");
        userDetailsList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                userDetailsListMouseReleased(evt);
            }
        });
        jScrollPane3.setViewportView(userDetailsList);

        jPanel2.add(jScrollPane3, java.awt.BorderLayout.CENTER);

        jPanel3.add(jPanel2, java.awt.BorderLayout.CENTER);

        usersPanel.add(jPanel3, java.awt.BorderLayout.CENTER);

        jPanel6.setBorder(javax.swing.BorderFactory.createTitledBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true), "User list", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 0))); // NOI18N
        jPanel6.setLayout(new java.awt.BorderLayout());

        companyUserList.setModel(userListModel);
        companyUserList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        companyUserList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                companyUserListMouseReleased(evt);
            }
        });
        companyUserList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                companyUserListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(companyUserList);

        jPanel6.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        usersPanel.add(jPanel6, java.awt.BorderLayout.WEST);

        updateButton.setText("Update");
        updateButton.setEnabled(false);
        updateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(usersPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 654, Short.MAX_VALUE)
                    .addComponent(companyNamePanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(companyAddressPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(updateButton))
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(companyNamePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(companyAddressPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(usersPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 210, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(updateButton)
                .addContainerGap())
        );

        companyScrollPane.setViewportView(jPanel5);

        add(companyScrollPane, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void streetAddressLabelMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_streetAddressLabelMouseReleased
    {//GEN-HEADEREND:event_streetAddressLabelMouseReleased
        if (evt.isPopupTrigger())
        {
            JPopupMenu Pmenu = new JPopupMenu();
            JMenuItem menuItem = new JMenuItem("Copy to postal address");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    if (newCompany == null)
                    {
                        newCompany = new Company();
                    }
                    newCompany.setPostalAddress(newCompany.getStreetAddress());
                    postalAddressLabel.setText(newCompany.getPostalAddress().toString());
                }
            });
            menuItem = new JMenuItem("Edit");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    editCompanyStreetAddressButtonActionPerformed(null);
                }
            });

            Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
}//GEN-LAST:event_streetAddressLabelMouseReleased

    private void postalAddressLabelMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_postalAddressLabelMouseReleased
    {//GEN-HEADEREND:event_postalAddressLabelMouseReleased
        if (evt.isPopupTrigger())
        {
            JPopupMenu Pmenu = new JPopupMenu();
            JMenuItem menuItem = new JMenuItem("Copy to street address");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    if (newCompany == null)
                    {
                        newCompany = new Company();
                    }
                    newCompany.setStreetAddress(newCompany.getPostalAddress());
                    streetAddressLabel.setText(newCompany.getStreetAddress().toString());
                }
            });
            menuItem = new JMenuItem("Edit");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    editCompanyPostalAddressButtonActionPerformed(null);
                }
            });

            Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
}//GEN-LAST:event_postalAddressLabelMouseReleased

    private void editCompanyStreetAddressButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_editCompanyStreetAddressButtonActionPerformed
    {//GEN-HEADEREND:event_editCompanyStreetAddressButtonActionPerformed
        if (newCompany == null)
        {
            newCompany = new Company();
        }
        AddressDialog ad = new AddressDialog(Constant.getParentFrame(this), true, "Street adress", "Street address 1:", "Street address 2:");
        ad.setAddress(newCompany.getStreetAddress());
        ad.setVisible(true);
        if (ad.isOk())
        {
            AddressPanel ap = ad.getAddressPanel();
            Address sAdd = newCompany.getStreetAddress();
            if (sAdd == null)
            {
                sAdd = new Address();
                newCompany.setStreetAddress(sAdd);
            }
            sAdd.setCity(ap.getCity());
            sAdd.setCode(ap.getCode());
            sAdd.setCountry(ap.getCountry());
            sAdd.setLine1(ap.getStreet1());
            sAdd.setLine2(ap.getStreet2());
            sAdd.setProvince(ap.getProvince());
            sAdd.setSuburb(ap.getSuburb());
            streetAddressLabel.setText(sAdd.toString());
        }
}//GEN-LAST:event_editCompanyStreetAddressButtonActionPerformed

    private void editCompanyPostalAddressButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_editCompanyPostalAddressButtonActionPerformed
    {//GEN-HEADEREND:event_editCompanyPostalAddressButtonActionPerformed
        if (newCompany == null)
        {
            newCompany = new Company();
        }
        AddressDialog ad = new AddressDialog(Constant.getParentFrame(this), true, "Postal adress", "Postal address 1:", "Postal address 2:");
        ad.setAddress(newCompany.getPostalAddress());
        ad.setVisible(true);
        if (ad.isOk())
        {
            AddressPanel ap = ad.getAddressPanel();
            Address pAdd = newCompany.getPostalAddress();
            if (pAdd == null)
            {
                pAdd = new Address();
                newCompany.setPostalAddress(pAdd);
            }
            pAdd.setCity(ap.getCity());
            pAdd.setCode(ap.getCode());
            pAdd.setCountry(ap.getCountry());
            pAdd.setLine1(ap.getStreet1());
            pAdd.setLine2(ap.getStreet2());
            pAdd.setProvince(ap.getProvince());
            pAdd.setSuburb(ap.getSuburb());
            postalAddressLabel.setText(pAdd.toString());
        }
}//GEN-LAST:event_editCompanyPostalAddressButtonActionPerformed

    private void userDetailsListMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_userDetailsListMouseReleased
    {//GEN-HEADEREND:event_userDetailsListMouseReleased
        if (evt.isPopupTrigger())
        {
            JPopupMenu Pmenu = new JPopupMenu();
            JMenuItem menuItem = new JMenuItem("New");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    editCompanyUserDetails(null);
                }
            });
            menuItem = new JMenuItem("Edit");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    editCompanyUserDetails(userDetailListModel.getUser());
                }
            });

            Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
}//GEN-LAST:event_userDetailsListMouseReleased

    private void companyUserListMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_companyUserListMouseReleased
    {//GEN-HEADEREND:event_companyUserListMouseReleased
        if (evt.isPopupTrigger())
        {
            JPopupMenu Pmenu = new JPopupMenu();
            JMenuItem menuItem = new JMenuItem("New");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    editCompanyUserDetails(null);
                }
            });
            menuItem = new JMenuItem("Edit");
            Pmenu.add(menuItem);
            menuItem.addActionListener(new ActionListener()
            {

                @Override
                public void actionPerformed(ActionEvent e)
                {
                    int k = companyUserList.getSelectedIndex();
                    if (k != -1)
                    {
                        UserLoginDetails uld = (UserLoginDetails) userListModel.getElementAt(k);
                        editCompanyUserDetails(uld);
                    }
                }
            });

            Pmenu.show(evt.getComponent(), evt.getX(), evt.getY());
        }
}//GEN-LAST:event_companyUserListMouseReleased

    private void companyUserListValueChanged(javax.swing.event.ListSelectionEvent evt)//GEN-FIRST:event_companyUserListValueChanged
    {//GEN-HEADEREND:event_companyUserListValueChanged
        int k = this.companyUserList.getSelectedIndex();
        if (k != -1)
        {
            userDetailListModel.setUser((UserLoginDetails) userListModel.getElementAt(k));
            this.userDetailsList.updateUI();
        }
}//GEN-LAST:event_companyUserListValueChanged

    private void copmanyNameTextFieldKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_copmanyNameTextFieldKeyPressed
    {//GEN-HEADEREND:event_copmanyNameTextFieldKeyPressed
        setDirty(true);
    }//GEN-LAST:event_copmanyNameTextFieldKeyPressed

    private void companyTradingAsTextFieldKeyPressed(java.awt.event.KeyEvent evt)//GEN-FIRST:event_companyTradingAsTextFieldKeyPressed
    {//GEN-HEADEREND:event_companyTradingAsTextFieldKeyPressed
        setDirty(true);
    }//GEN-LAST:event_companyTradingAsTextFieldKeyPressed

    private void companyEnabledCheckBoxStateChanged(javax.swing.event.ChangeEvent evt)//GEN-FIRST:event_companyEnabledCheckBoxStateChanged
    {//GEN-HEADEREND:event_companyEnabledCheckBoxStateChanged
        
    }//GEN-LAST:event_companyEnabledCheckBoxStateChanged

    private void companySubscribeCheckBoxStateChanged(javax.swing.event.ChangeEvent evt)//GEN-FIRST:event_companySubscribeCheckBoxStateChanged
    {//GEN-HEADEREND:event_companySubscribeCheckBoxStateChanged
        
    }//GEN-LAST:event_companySubscribeCheckBoxStateChanged

    private void updateButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_updateButtonActionPerformed
    {//GEN-HEADEREND:event_updateButtonActionPerformed
        if(newCompany == null)
        {
            newCompany = new Company();
        }
        newCompany.setName(copmanyNameTextField.getText().trim().length() == 0 ? null : copmanyNameTextField.getText().trim());
        newCompany.setTradingAs(companyTradingAsTextField.getText().trim().length() == 0 ? null : companyTradingAsTextField.getText().trim());
        newCompany.setEnabled(this.companyEnabledCheckBox.isSelected());
        newCompany.setSubscriber(this.companySubscribeCheckBox.isSelected());
        UserDetailModel udm = (UserDetailModel) Constant.mvc.getModel(BusinessMVC.USER_DETAIL_MODEL);
        companyDetailController.SaveCompany(newCompany, udm.getCurrentUser());
    }//GEN-LAST:event_updateButtonActionPerformed

    private void companySubscribeCheckBoxMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_companySubscribeCheckBoxMouseReleased
    {//GEN-HEADEREND:event_companySubscribeCheckBoxMouseReleased
        setDirty(true);
    }//GEN-LAST:event_companySubscribeCheckBoxMouseReleased

    private void companyEnabledCheckBoxMouseReleased(java.awt.event.MouseEvent evt)//GEN-FIRST:event_companyEnabledCheckBoxMouseReleased
    {//GEN-HEADEREND:event_companyEnabledCheckBoxMouseReleased
        setDirty(true);
    }//GEN-LAST:event_companyEnabledCheckBoxMouseReleased

    @Override
    public void modelPropertyChange(PropertyChangeEvent evt)
    {
        if (evt.getPropertyName().compareTo(MessageObj.REQUEST_COMPANY_DETAILS_CHANGE) == 0)
        {
            if ((evt.getNewValue() != null) && (evt.getNewValue() instanceof Company))
            {
                Company currentCompany = (Company) evt.getOldValue();
                Company sendNewCompany = (Company) evt.getNewValue();
                if (checkCompany(currentCompany) == JOptionPane.NO_OPTION)
                {
                    try
                    {
                        setDirty(false);
                        newCompany = sendNewCompany.clone();
                        //newCompany.resetDirty();
                        newCompany.setReadyToChange(true);
                        companyDetailController.makeNewCompany(sendNewCompany);
                    }
                    catch (Exception ex)
                    {
                        ex.printStackTrace();
                    }
                    updateCompanyFields();
                }
            }
        }
        else if (evt.getPropertyName().compareTo(MessageObj.COMPANY_DETAILS_CHANGED) == 0)
        {
            System.out.println("Maybe handle this???!!!");
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel companyAddressPanel;
    private javax.swing.JLabel companyCodeLabel;
    private javax.swing.JCheckBox companyEnabledCheckBox;
    private javax.swing.JPanel companyNamePanel;
    private javax.swing.JScrollPane companyScrollPane;
    private javax.swing.JCheckBox companySubscribeCheckBox;
    private javax.swing.JTextField companyTradingAsTextField;
    private javax.swing.JList companyUserList;
    private javax.swing.JTextField copmanyNameTextField;
    private javax.swing.JButton editCompanyPostalAddressButton;
    private javax.swing.JButton editCompanyStreetAddressButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JLabel postalAddressLabel;
    private javax.swing.JLabel streetAddressLabel;
    private javax.swing.JButton updateButton;
    private javax.swing.JList userDetailsList;
    private javax.swing.JPanel usersPanel;
    // End of variables declaration//GEN-END:variables

    private void editCompanyUserDetails(UserLoginDetails userLoginDetails)
    {
        UserDialog ud = new UserDialog(Constant.getParentFrame(this), true);
        ud.setUserLoginDetails(userLoginDetails);
        ud.setVisible(true);
        if (ud.isOk())
        {
            if (newCompany == null)
            {
                newCompany = new Company();
            }
            UserLoginDetails uld = ud.getUserLoginDetails();
            if (userLoginDetails == null)
            {
                userLoginDetails = new UserLoginDetails();
                newCompany.add(userLoginDetails);
                userLoginDetails.add(newCompany);
            }
            userLoginDetails.setContactPerson(uld.getContactPerson());
            userLoginDetails.setEnabled(uld.getEnabled());
            userLoginDetails.setPassword(uld.getPassword());
            userLoginDetails.setUserGroup(uld.getUserGroup());
            userLoginDetails.setUserName(uld.getUserName());
            userLoginDetails.setUserType(uld.getUserType());
            setDirty(true);
            this.userListModel.setUsers(newCompany.getUsers());
            this.companyUserList.updateUI();
            userDetailsList.updateUI();
        }
    }

    private void updateCompanyFields()
    {
        if (newCompany == null)
        {
            newCompany = new Company();
        }
        this.companyCodeLabel.setText(newCompany.getCode());
        this.copmanyNameTextField.setText(newCompany.getName());
        this.companyTradingAsTextField.setText(newCompany.getTradingAs());
        if (newCompany.getStreetAddress() != null)
        {
            this.streetAddressLabel.setText(newCompany.getStreetAddress().toString());
        }
        else
        {
            this.streetAddressLabel.setText("");
        }
        if (newCompany.getPostalAddress() != null)
        {
            this.postalAddressLabel.setText(newCompany.getPostalAddress().toString());
        }
        else
        {
            this.postalAddressLabel.setText("");
        }
        this.userListModel.setUsers(newCompany.getUsers());
        userDetailListModel.setUser((UserLoginDetails) userListModel.getElementAt(0));
        this.companyEnabledCheckBox.setSelected(newCompany.getEnabled());
        this.companySubscribeCheckBox.setSelected(newCompany.getSubscriber());
        this.companyUserList.updateUI();
        this.userDetailsList.updateUI();
        setDirty(false);
    }

    private void updateData()
    {
        if (newCompany == null)
        {
            return;
        }
        newCompany.setName(copmanyNameTextField.getText().trim().length() == 0 ? null : copmanyNameTextField.getText().trim());
        newCompany.setTradingAs(companyTradingAsTextField.getText().trim().length() == 0 ? null : companyTradingAsTextField.getText().trim());
        newCompany.setEnabled(this.companyEnabledCheckBox.isSelected());
        newCompany.setSubscriber(this.companySubscribeCheckBox.isSelected());
    }

    public int checkCompany(Company sendNewCompany)
    {
        //first set the text field data to newCompany fields
        updateData();
        if ((newCompany == null) && (!isDirty()))
        {
            return JOptionPane.NO_OPTION;
        }
        else if ((newCompany == null) && (isDirty()))
        {
            int resp = JOptionPane.showConfirmDialog(this, "Save changes?", "Unsaved data", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (resp == JOptionPane.YES_OPTION)
            {
                UserDetailModel udm = (UserDetailModel) Constant.mvc.getModel(BusinessMVC.USER_DETAIL_MODEL);
                companyDetailController.SaveCompany(newCompany, udm.getCurrentUser());
            }
            return resp;
        }
        else if ((newCompany != null) && (!newCompany.check(sendNewCompany)))
        {
            int resp = JOptionPane.showConfirmDialog(this, "Save changes?", "Unsaved data", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (resp == JOptionPane.YES_OPTION)
            {
                UserDetailModel udm = (UserDetailModel) Constant.mvc.getModel(BusinessMVC.USER_DETAIL_MODEL);
                companyDetailController.SaveCompany(newCompany, udm.getCurrentUser());
            }
            return resp;
        }
        return JOptionPane.NO_OPTION;
    }

    /**
     * @return the dirty
     */
    @Override
    public boolean isDirty()
    {
        return dirty;
    }

    /**
     * @param dirty the dirty to set
     */
    public void setDirty(boolean dirty)
    {
        this.dirty = dirty;
        if(this.dirty)
        {
            updateButton.setEnabled(true);
        }
        else
        {
            updateButton.setEnabled(false);
        }
    }

    @Override
    public String getExtraTitle()
    {
        if(newCompany != null)
        {
            return newCompany.getCode();
        }
        else
        {
            return "[NEW]";
        }
    }
}
